home *** CD-ROM | disk | FTP | other *** search
/ Computer Music Interactif…cial Edition 1999 Winter / cd 3.iso / pc / Mac / Shares / Midishare™1.68 / Development Tools / Libraries / MidiFiles / MidiFile.txt < prev    next >
Encoding:
Text File  |  1996-04-18  |  15.1 KB  |  865 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7. MidiShare Functions Library
  8.  
  9.  
  10.  
  11.  
  12.  
  13. MIDI Files Management
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29. GRAME Research Lab.
  30.  
  31. 6 quai Jean Moulin - BP 1185
  32.  
  33. 69202 LYON CEDEX 01
  34.  
  35. Ph: (33) 72.07.37.00 Fax: (33) 72.07.37.01
  36.  
  37.  
  38.  
  39. e-mail : GRAME@rd.grame.fr
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51. Functions Summary
  52.  
  53.  
  54.  
  55.  
  56.  
  57. MidiFileGetVersion      return the version numbers
  58.  
  59.  
  60.  
  61. MidiFileOpen    open an existing MIDI file
  62.  
  63. MidiFileClose   close a file opened with MidiFileOpen
  64.  
  65. MidiFileCreate  create a MIDI file
  66.  
  67.  
  68.  
  69. MidiFileOpenTrack       open an existing track
  70.  
  71. MidiFileNewTrack        create a new track
  72.  
  73. MidiFileCloseTrack      close a track
  74.  
  75. MidiFileSetPos  locate to the beginning of a track
  76.  
  77.  
  78.  
  79. MidiFileReadEv  read an event within the current track
  80.  
  81. MidiFileReadTrack       read a track
  82.  
  83.  
  84.  
  85. MidiFileWriteEv         write an event to the current track
  86.  
  87. MidiFileWriteTrack      write a sequence to a track
  88.  
  89.  
  90.  
  91. MidiFileGetMFErrno        returns the MidiFile_errno code
  92.  
  93. MidiFileGetErrno            returns the errno code
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101. Warning: all these functions doesn't check for datas consistency,  
  102.  
  103. according to the MIDIFile specifications. 
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119. MidiFileChooseTrack
  120.  
  121. Function
  122.  
  123. Synopsis
  124.  
  125. #include <MidiFile.h>
  126.  
  127. Boolean MidiFileChooseTrack( midiFILE *fd, short numTrack);
  128.  
  129.  
  130.  
  131. Description
  132.  
  133. Locate at the beginning of the track numTrack within the file pointed to by fd.
  134.  
  135. An error occur if the function returns false:
  136.  
  137.         it is a MidiShare error if MidiFile_errno is not equal to MidiFileNoErr,
  138.  
  139.         otherwise, the error code is in errno.
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147. MidiFileClose
  148.  
  149. Function
  150.  
  151. Synopsis
  152.  
  153. #include <MidiFile.h>
  154.  
  155. Boolean MidiFileClose( midiFILE *fd);
  156.  
  157.  
  158.  
  159. Description
  160.  
  161. Close the file pointed to by fd, previously opened with MidiFileOpen or MidiFileCreate. 
  162.  
  163. If a track is still opened, the function closes it with a call to MidiFileCloseTrack.
  164.  
  165.  
  166.  
  167. see also : MidiFileOpen MidiFileCreate
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175. MidiFileCloseTrack
  176.  
  177. Function
  178.  
  179. Synopsis
  180.  
  181. #include <MidiFile.h>
  182.  
  183. Boolean   MidiFileCloseTrack( midiFILE *fd);
  184.  
  185.  
  186.  
  187. Description
  188.  
  189. Close a track previously opened with MidiFileOpenTrack or created with MidiFileNewTrack. 
  190.  
  191. - If the file is opened for reading, MidiFileCloseTrack locate the file pointeur at the 
  192.  
  193.   beginning of the next track.
  194.  
  195. - If the file is opened for writing, MidiFileCloseTrack flush the KeyOff sequence (coming 
  196.  
  197.   from typeNote events), update the track header and the file header.
  198.  
  199. The function does nothing and returns true if the track is still closed.
  200.  
  201.  
  202.  
  203. An error occur if the function returns false:
  204.  
  205.         it is a MidiShare error if MidiFile_errno is not equal to MidiFileNoErr,
  206.  
  207.         otherwise, the error code is in errno.
  208.  
  209.  
  210.  
  211. see also : MidiFileOpenTrack MidiFileNewTrack
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221. MidiFileCreate
  222.  
  223. Function
  224.  
  225. Synopsis
  226.  
  227. #include <MidiFile.h>
  228.  
  229. midiFILE *MidiFileCreate( const char *filename, short format, short timeDef, short ticks);
  230.  
  231.  
  232.  
  233. Description
  234.  
  235. Create a MIDIFile format file. The function parameters are as follow:
  236.  
  237.         filename        :  name of the file to create.
  238.  
  239.         format  :  MIDIFile format of the file, it can takes the following values:
  240.  
  241.                                 - midifile0 : format 0 (one track)
  242.  
  243.                                 - midifile1 : format 1 (several tracks, to read according
  244.  
  245.                                               to the tempo map contained in the track #0)
  246.  
  247.                                 - midifile2 : format 2 (several independant patterns one per track, 
  248.  
  249.                                               every track contains its own tempo map)
  250.  
  251.         timeDef : specify the time representation, it can takes the following values:
  252.  
  253.                                         - TicksPerQuarterNote   : MIDI measured time.
  254.  
  255.                                                 - Smpte24               : smpte time 24 frame/sec.
  256.  
  257.                                                 - Smpte25               : smpte time 25 frame/sec.
  258.  
  259.                                                 - Smpte29               : smpte time 29 frame/sec.
  260.  
  261.                                                 - Smpte30               : smpte time 30 frame/sec.
  262.  
  263.         ticks           : for MIDI time: represents the ticks count per quarter note.
  264.  
  265.                           for smpte time: represents the ticks count per frame.
  266.  
  267.  
  268.  
  269. The function returns a pointer to the following structure:
  270.  
  271.         typedef struct midiFILE{
  272.  
  273.                 short   format;                 /* file format                          */
  274.  
  275.                 unsigned short ntrks;   /* track count                          */
  276.  
  277.                 short   time;                   /* time representation :                        */
  278.  
  279.                                         /* for MIDI time: tick count per quarter note   */
  280.  
  281.                                         /* for smpte time: b. 15  = 1                     */
  282.  
  283.                                         /*                 b.8-14 = frame count per sec   */
  284.  
  285.                                         /*                b.0-7  = tick count per frame  */
  286.  
  287.                 FILE    *fd;                    /* standard file descriptor             */      
  288.  
  289.                 fpos_t  trkHeadOffset;  /* track header offset                  */
  290.  
  291.                                                                 /* nil if the track is closed           */
  292.  
  293.                 long    _cnt;
  294.  
  295.                 MidiSeqPtr keyOff;      /* keyOff coming from typeNote events   */
  296.  
  297.                 long    curDate;        /* current date                         */
  298.  
  299.                 Boolean mode;           /* 0/1 : reading/writing                */
  300.  
  301.         }midiFILE;
  302.  
  303.  
  304.  
  305. An error occur if the function returns null:
  306.  
  307.         it is a MidiShare error if MidiFile_errno is not equal to MidiFileNoErr,
  308.  
  309.         otherwise, the error code is in errno.
  310.  
  311.  
  312.  
  313. see also : MidiFileOpen MidiFileClose
  314.  
  315.  
  316.  
  317. MidiFileGetVersion
  318.  
  319. Function
  320.  
  321. Synopsis
  322.  
  323. #include <MidiFile.h>
  324.  
  325. const MDF_versions *MidiFileGetVersion(void);
  326.  
  327.  
  328.  
  329. Description
  330.  
  331. Return the version number of the implemented MIDIFile format and of  the source code.
  332.  
  333.  
  334.  
  335. MidiFileGetVersion returns a pointer on the following structure:
  336.  
  337.         typedef struct MDF_versions{
  338.  
  339.                 short   src;                            /* source code version              */
  340.  
  341.                 short   MidiFile;                       /* MIDIFile format version          */
  342.  
  343.         }MDF_versions;
  344.  
  345.  
  346.  
  347.  
  348.  
  349. MidiFileNewTrack
  350.  
  351. Function
  352.  
  353. Synopsis
  354.  
  355. #include <MidiFile.h>
  356.  
  357. Boolean  MidiFileNewTrack( midiFILE *fd);
  358.  
  359.  
  360.  
  361. Description
  362.  
  363. MidiFileNewTrack adds a new track header at the end of the file and open the 
  364.  
  365. corresponding track. You can use this function only if the file is opened for writing. 
  366.  
  367. A previously opened track will first been closed.
  368.  
  369.  
  370.  
  371. An error occur if the function returns false:
  372.  
  373.         it is a MidiShare error if MidiFile_errno is not equal to MidiFileNoErr,
  374.  
  375.         otherwise, the error code is in errno.
  376.  
  377.  
  378.  
  379. see also : MidiFileOpenTrack MidiFileCloseTrack
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387. MidiFileOpen
  388.  
  389. Function
  390.  
  391. Synopsis
  392.  
  393. #include <MidiFile.h>
  394.  
  395. midiFILE *MidiFileOpen( const char *filename, short mode);
  396.  
  397.  
  398.  
  399. Description
  400.  
  401. Open an existing MIDI file. The function parameters are as follow:
  402.  
  403.         filename        :  name of the file.
  404.  
  405.         mode            :  can takes the following values :
  406.  
  407.                                                 - MidiFileRead          : to read the file
  408.  
  409.                                                 - MidiFileAppend        : to append to the file
  410.  
  411.  
  412.  
  413. The function returns a pointer to the following structure:
  414.  
  415.         typedef struct midiFILE{
  416.  
  417.                 short   format;                 /* file format                          */
  418.  
  419.                 unsigned short ntrks;   /* track count                          */
  420.  
  421.                 short   time;                   /* time representation :                        */
  422.  
  423.                                         /* for MIDI time: tick count per quarter note   */
  424.  
  425.                                         /* for smpte time: b. 15  = 1                     */
  426.  
  427.                                         /*                 b.8-14 = frame count per sec   */
  428.  
  429.                                         /*                b.0-7  = tick count per frame  */
  430.  
  431.                 FILE    *fd;                    /* standard file descriptor             */      
  432.  
  433.                 fpos_t  trkHeadOffset;  /* track header offset                  */
  434.  
  435.                                                                 /* nil if the track is closed           */
  436.  
  437.                 long    _cnt;
  438.  
  439.                 MidiSeqPtr keyOff;              /* keyOff coming from typeNote events   */
  440.  
  441.                 long    curDate;                /* current date                         */
  442.  
  443.                 Boolean mode;                   /* 0/1 : reading/writing                */
  444.  
  445.         }midiFILE;
  446.  
  447.  
  448.  
  449. An error occur if the function returns nil:
  450.  
  451.         it is a MidiShare error if MidiFile_errno is not equal to MidiFileNoErr,
  452.  
  453.         otherwise, the error code is in errno.
  454.  
  455.  
  456.  
  457. see also : MidiFileClose MidiFileCreate
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469. MidiFileOpenTrack
  470.  
  471. Function
  472.  
  473. Synopsis
  474.  
  475. #include <MidiFile.h>
  476.  
  477. Boolean  MidiFileOpenTrack( midiFILE *fd);
  478.  
  479.  
  480.  
  481. Description
  482.  
  483. Open the track if the file is opened for reading, otherwise the function returns 
  484.  
  485. false and MidiFile_errno is set to MidiFileErrNoTrack.  The function does nothing 
  486.  
  487. and returns true if the track is still opened. The purpose of this function consists 
  488.  
  489. essentially in data initialization to facilitate the track handling.
  490.  
  491.  
  492.  
  493. see also : MidiFileNewTrack MidiFileCloseTrack
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501. MidiFileReadEv
  502.  
  503. Function
  504.  
  505. Synopsis
  506.  
  507. #include <MidiFile.h>
  508.  
  509. MidiEvPtr  MidiFileReadEv( midiFILE *fd);
  510.  
  511.  
  512.  
  513. Description
  514.  
  515. MidiFileReadEv returns the next event within the current track. The track must be 
  516.  
  517. opened using MidiFileOpenTrack before reading an event. When you reach the end of the 
  518.  
  519. current track, it is automaticaly closed and the function returns nil.
  520.  
  521.  
  522.  
  523. An error occur if the function returns nil when the track is still opened:
  524.  
  525.         it is a MidiShare error if MidiFile_errno is not equal to MidiFileNoErr,
  526.  
  527.         otherwise, the error code is in errno.
  528.  
  529.  
  530.  
  531. see also : MidiFileReadTrack isTrackOpen
  532.  
  533.  
  534.  
  535.  
  536.  
  537.  
  538.  
  539. MidiFileReadTrack
  540.  
  541. Function
  542.  
  543. Synopsis
  544.  
  545. #include <MidiFile.h>
  546.  
  547. MidiSeqPtr MidiFileReadTrack( midiFILE *fd);
  548.  
  549.  
  550.  
  551. Description
  552.  
  553. The function reads the current track from the file and returns the result in a MidiShare 
  554.  
  555. sequence. MidiFileReadTrack automaticaly opens and closes the track to read.
  556.  
  557.  
  558.  
  559. An error occur if the function returns nil when the track is still opened:
  560.  
  561.         it is a MidiShare error if MidiFile_errno is not equal to MidiFileNoErr,
  562.  
  563.         otherwise, the error code is in errno.
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581. MidiFileWriteEv
  582.  
  583. Function
  584.  
  585. Synopsis
  586.  
  587. #include <MidiFile.h>
  588.  
  589. Boolean    MidiFileWriteEv( midiFILE *fd, MidiEvPtr ev);
  590.  
  591.  
  592.  
  593. Description
  594.  
  595. MidiFileWriteEv writes the event ev to the current track. The track must be previously 
  596.  
  597. opened using the MidiFileNewTrack function.
  598.  
  599.  
  600.  
  601. An error occur if the function returns false:
  602.  
  603.         it is a MidiShare error if MidiFile_errno is not equal to MidiFileNoErr,
  604.  
  605.         otherwise, the error code is in errno.
  606.  
  607.  
  608.  
  609. see also : MidiFileWriteTrack
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616.  
  617. MidiFileWriteTrack
  618.  
  619. Function
  620.  
  621. Synopsis
  622.  
  623. #include <MidiFile.h>
  624.  
  625. Boolean MidiFileWriteTrack( midiFILE *fd, MidiSeqPtr seq);
  626.  
  627.  
  628.  
  629. Description
  630.  
  631. Writes in order all the events of the sequence seq to the file pointed to by fd.
  632.  
  633. MidiFileWriteTrack automaticaly create and close the written track. 
  634.  
  635.  
  636.  
  637. An error occur if the function returns false:
  638.  
  639.         it is a MidiShare error if MidiFile_errno is not equal to MidiFileNoErr,
  640.  
  641.         otherwise, the error code is in errno.
  642.  
  643.  
  644.  
  645. see also : MidiFileWriteEv
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653. isTrackOpen
  654.  
  655. macro
  656.  
  657.  
  658.  
  659. #include <MidiFile.h>
  660.  
  661.  
  662.  
  663. #ifdef __cplusplus
  664.  
  665.         inline Boolean isTrackOpen( midiFILE *fd) 
  666.  
  667.                                                 { return (fd->trkHeadOffset> 0) }
  668.  
  669. #else
  670.  
  671.         #define isTrackOpen(fd) (fd->trkHeadOffset > 0)
  672.  
  673. #endif
  674.  
  675.  
  676.  
  677. Description
  678.  
  679. Returns the current track state: closed or opened.
  680.  
  681.  
  682.  
  683.  
  684.  
  685.  
  686.  
  687. MidiFileGetMFErrno
  688.  
  689. Function
  690.  
  691. Synopsis
  692.  
  693. #include <MidiFile.h>
  694.  
  695. int MFAPI MidiFileGetMFErrno (void);
  696.  
  697.  
  698.  
  699. Description
  700.  
  701. returns the MidiFile_errno code
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709. MidiFileGetErrno
  710.  
  711. Function
  712.  
  713. Synopsis
  714.  
  715. #include <MidiFile.h>
  716.  
  717. int MFAPI MidiFileGetErrno (void);
  718.  
  719.  
  720.  
  721. Description
  722.  
  723. returns the errno code
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.  
  731. Source code examples
  732.  
  733.  
  734.  
  735. A typical example of code to read a MIDI file might be the following one:
  736.  
  737.  
  738.  
  739. MidiSeqPtr ReadMIDIFile( char *itsName)
  740.  
  741. {
  742.  
  743.         MidiSeqPtr seq, tmp;
  744.  
  745.         midiFILE *fd;
  746.  
  747.         unsigned short n;
  748.  
  749.         
  750.  
  751.         seq= MidiNewSeq()                               /* allocate a new MidiShare sequence */
  752.  
  753.         if( fd= MidiFileOpen( itsName, MidiFileRead))
  754.  
  755.         {
  756.  
  757.                 n= fd->ntrks;                           /* get the number of tracks     */
  758.  
  759.                 while( n--) {
  760.  
  761.                         tmp= MidiFileReadTrack( fd);    /* read every track             */
  762.  
  763.  
  764.  
  765.                         Mix( tmp, seq);                 /* the Mix function is to provide*/
  766.  
  767.                                                                         /* it transfers the content of the first        */
  768.  
  769.                                                                         /* sequence to the second one,          */
  770.  
  771.                                                                         /* its interface might be :             */
  772.  
  773.                                                                         /* void Mix( MidiSeqPtr src, MidiSeqPtr dst)*/
  774.  
  775.  
  776.  
  777.                         MidiFreeSeq( tmp);      /* this sequence is now empty, we can   */
  778.  
  779.                                                                 /* free it without freing the readed    */
  780.  
  781.                                                                 /* events                               */
  782.  
  783.                 }
  784.  
  785.                 MidiFileClose( fd);
  786.  
  787.         }
  788.  
  789.         return seq;
  790.  
  791. }
  792.  
  793.  
  794.  
  795.  
  796.  
  797.  A typical example of code to create a format 1 MIDI file might be the following one:
  798.  
  799.  
  800.  
  801. void WriteMIDIFile( char *itsName)
  802.  
  803. {
  804.  
  805.         midiFILE *fd;
  806.  
  807.  
  808.  
  809.         /* we first create a new MIDI file using a format 1     */
  810.  
  811.         if( fd= MidiFileCreate( itsName, midifile1, TicksPerQuarterNote, 500))
  812.  
  813.         {
  814.  
  815.                 /* for the file consistency, the first track    */
  816.  
  817.                 /* to write is the tempo map                    */                              
  818.  
  819.                 MidiFileWriteTrack( fd, myTempoMap);
  820.  
  821.  
  822.  
  823.                 /* then we can write all the other tracks       */
  824.  
  825.                                         /* 
  826.  
  827.                                            it is the program responsibility to determine 
  828.  
  829.                                            the content of the tracks. Here, every track is
  830.  
  831.                                            stored in separate MidiShare sequences (myTempoMap,
  832.  
  833.                                            track1, track2,...trackn). They are supposed to be
  834.  
  835.                                            global variables. Of course, events in the file will
  836.  
  837.                                            keep exactly the same order than in the sequence
  838.  
  839.                                          */
  840.  
  841.                 MidiFileWriteTrack( fd, track1);
  842.  
  843.                 MidiFileWriteTrack( fd, track2);
  844.  
  845.                 /* ...*/
  846.  
  847.                 MidiFileWriteTrack( fd, trackn);
  848.  
  849.                 
  850.  
  851.                 /* and we finaly close the file                 */
  852.  
  853.                 MidiFileClose( fd);
  854.  
  855.         }
  856.  
  857. }
  858.  
  859.  
  860.  
  861. Warning! take care of that these examples doesn't check for errors
  862.  
  863.  
  864.  
  865.